﻿namespace System.Globalization
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Drawing;
    using System.Reflection;
    using System.Resources;
    using System.Threading;
    using System.Windows.Forms;

    public class FormLanguageSwitchSingleton
    {
        private CultureInfo m_cultureInfo;
        protected static readonly FormLanguageSwitchSingleton m_instance = new FormLanguageSwitchSingleton();

        protected FormLanguageSwitchSingleton()
        {
        }

        public void ChangeCurrentThreadUICulture(CultureInfo cultureInfo)
        {
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }

        private void ChangeFormLanguage(Form form)
        {
            form.SuspendLayout();
            Cursor.Current = Cursors.WaitCursor;
            ResourceManager resources = new ResourceManager(form.GetType());
            form.Text = (string) this.GetSafeValue(resources, "$this.Text", form.Text);
            this.ReloadControlCommonProperties(form, resources);
            ToolTip toolTip = this.GetToolTip(form);
            this.RecurControls(form, resources, toolTip);
            this.ScanNonControls(form, resources);
            form.ResumeLayout();
        }

        public void ChangeLanguage(Form form)
        {
            this.ChangeLanguage(form, Thread.CurrentThread.CurrentUICulture);
        }

        public void ChangeLanguage(Form form, CultureInfo cultureInfo)
        {
            this.m_cultureInfo = cultureInfo;
            this.ChangeFormLanguage(form);
            foreach (Form form2 in form.MdiChildren)
            {
                this.ChangeFormLanguage(form2);
            }
        }

        private object GetSafeValue(ResourceManager resources, string name, object currentValue)
        {
            object obj2 = resources.GetObject(name, this.m_cultureInfo);
            if (obj2 == null)
            {
                Trace.WriteLine(string.Format("Resource for {0} not found, using current value.", name));
                return currentValue;
            }
            return obj2;
        }

        private ToolTip GetToolTip(Control control)
        {
            Debug.Assert((control is Form) || (control is UserControl));
            FieldInfo[] fields = control.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                object obj2 = fields[i].GetValue(control);
                if (obj2 is ToolTip)
                {
                    return (ToolTip) obj2;
                }
            }
            return null;
        }

        private void RecurControls(Control parent, ResourceManager resources, ToolTip toolTip)
        {
            foreach (Control control in parent.Controls)
            {
                control.SuspendLayout();
                this.ReloadControlCommonProperties(control, resources);
                this.ReloadControlSpecificProperties(control, resources);
                if (toolTip != null)
                {
                    toolTip.SetToolTip(control, (string) this.GetSafeValue(resources, control.Name + ".ToolTip", control.Text));
                }
                if (control is UserControl)
                {
                    this.RecurUserControl((UserControl) control);
                }
                else
                {
                    this.ReloadTextForSelectedControls(control, resources);
                    this.ReloadListItems(control, resources);
                    if (control is TreeView)
                    {
                        this.ReloadTreeViewNodes((TreeView) control, resources);
                    }
                    if (control.Controls.Count > 0)
                    {
                        this.RecurControls(control, resources, toolTip);
                    }
                }
                control.ResumeLayout();
            }
        }

        private void RecurUserControl(UserControl userControl)
        {
            ResourceManager resources = new ResourceManager(userControl.GetType());
            ToolTip toolTip = this.GetToolTip(userControl);
            this.RecurControls(userControl, resources, toolTip);
            this.ScanNonControls(userControl, resources);
        }

        private void ReloadComboBoxItems(ComboBox comboBox, ResourceManager resources)
        {
            if (comboBox.Items.Count > 0)
            {
                int selectedIndex = comboBox.SelectedIndex;
                this.ReloadItems(comboBox.Name, comboBox.Items, comboBox.Items.Count, resources);
                if (!comboBox.Sorted)
                {
                    comboBox.SelectedIndex = selectedIndex;
                }
            }
        }

        protected virtual void ReloadControlCommonProperties(Control control, ResourceManager resources)
        {
            this.SetProperty(control, "AccessibleDescription", resources);
            this.SetProperty(control, "AccessibleName", resources);
            this.SetProperty(control, "BackgroundImage", resources);
            this.SetProperty(control, "Font", resources);
            this.SetProperty(control, "ImeMode", resources);
            this.SetProperty(control, "RightToLeft", resources);
            this.SetProperty(control, "Size", resources);
            if (!(control is Form))
            {
                this.SetProperty(control, "Anchor", resources);
                this.SetProperty(control, "Dock", resources);
                this.SetProperty(control, "Enabled", resources);
                this.SetProperty(control, "Location", resources);
                this.SetProperty(control, "TabIndex", resources);
                this.SetProperty(control, "Visible", resources);
            }
            if (control is ScrollableControl)
            {
                this.ReloadScrollableControlProperties((ScrollableControl) control, resources);
                if (control is Form)
                {
                    this.ReloadFormProperties((Form) control, resources);
                }
            }
        }

        protected virtual void ReloadControlSpecificProperties(Control control, ResourceManager resources)
        {
            this.SetProperty(control, "ImageIndex", resources);
            this.SetProperty(control, "ToolTipText", resources);
            this.SetProperty(control, "IntegralHeight", resources);
            this.SetProperty(control, "ItemHeight", resources);
            this.SetProperty(control, "MaxDropDownItems", resources);
            this.SetProperty(control, "MaxLength", resources);
            this.SetProperty(control, "Appearance", resources);
            this.SetProperty(control, "CheckAlign", resources);
            this.SetProperty(control, "FlatStyle", resources);
            this.SetProperty(control, "ImageAlign", resources);
            this.SetProperty(control, "Indent", resources);
            this.SetProperty(control, "Multiline", resources);
            this.SetProperty(control, "BulletIndent", resources);
            this.SetProperty(control, "RightMargin", resources);
            this.SetProperty(control, "ScrollBars", resources);
            this.SetProperty(control, "WordWrap", resources);
            this.SetProperty(control, "ZoomFactor", resources);
            this.SetProperty(control, "ButtonSize", resources);
            this.SetProperty(control, "DropDownArrows", resources);
            this.SetProperty(control, "ShowToolTips", resources);
            this.SetProperty(control, "Wrappable", resources);
            this.SetProperty(control, "AutoSize", resources);
        }

        private void ReloadFormProperties(Form form, ResourceManager resources)
        {
            this.SetProperty(form, "AutoScaleBaseSize", resources);
            this.SetProperty(form, "Icon", resources);
            this.SetProperty(form, "MaximumSize", resources);
            this.SetProperty(form, "MinimumSize", resources);
        }

        private void ReloadItems(string controlName, IList list, int itemsNumber, ResourceManager resources)
        {
            string name = controlName + ".Items";
            object obj2 = resources.GetString(name, this.m_cultureInfo);
            if (obj2 == null)
            {
                name = name + ".Items";
                obj2 = resources.GetString(name, this.m_cultureInfo);
            }
            if (obj2 != null)
            {
                list.Clear();
                Debug.Assert(obj2 != null);
                list.Add(obj2);
                for (int i = 1; i < itemsNumber; i++)
                {
                    list.Add(resources.GetString(name + i, this.m_cultureInfo));
                }
            }
        }

        private void ReloadListBoxItems(ListBox listBox, ResourceManager resources)
        {
            if (listBox.Items.Count > 0)
            {
                int[] destination = new int[listBox.SelectedIndices.Count];
                listBox.SelectedIndices.CopyTo(destination, 0);
                this.ReloadItems(listBox.Name, listBox.Items, listBox.Items.Count, resources);
                if (!listBox.Sorted)
                {
                    for (int i = 0; i < destination.Length; i++)
                    {
                        listBox.SetSelected(destination[i], true);
                    }
                }
            }
        }

        protected virtual void ReloadListItems(Control control, ResourceManager resources)
        {
            if (control is ComboBox)
            {
                this.ReloadComboBoxItems((ComboBox) control, resources);
            }
            else if (control is ListBox)
            {
                this.ReloadListBoxItems((ListBox) control, resources);
            }
            else if (control is DomainUpDown)
            {
                this.ReloadUpDownItems((DomainUpDown) control, resources);
            }
        }

        private void ReloadScrollableControlProperties(ScrollableControl control, ResourceManager resources)
        {
            this.SetProperty(control, "AutoScroll", resources);
            this.SetProperty(control, "AutoScrollMargin", resources);
            this.SetProperty(control, "AutoScrollMinSize", resources);
        }

        protected virtual void ReloadTextForSelectedControls(Control control, ResourceManager resources)
        {
            if (((((control is AxHost) || (control is ButtonBase)) || ((control is GroupBox) || (control is Label))) || (((control is ScrollableControl) || (control is StatusBar)) || (control is TabControl))) || (control is ToolBar))
            {
                control.Text = (string) this.GetSafeValue(resources, control.Name + ".Text", control.Text);
            }
        }

        private void ReloadTreeViewNodes(TreeView treeView, ResourceManager resources)
        {
            if (treeView.Nodes.Count > 0)
            {
                string name = treeView.Name + ".Nodes";
                TreeNode[] nodes = new TreeNode[treeView.Nodes.Count];
                nodes[0] = (TreeNode) resources.GetObject(name, this.m_cultureInfo);
                if (nodes[0] == null)
                {
                    name = name + ".Nodes";
                    nodes[0] = (TreeNode) resources.GetObject(name, this.m_cultureInfo);
                }
                Debug.Assert(nodes[0] != null);
                for (int i = 1; i < treeView.Nodes.Count; i++)
                {
                    nodes[i] = (TreeNode) resources.GetObject(name + i.ToString(), this.m_cultureInfo);
                }
                treeView.Nodes.Clear();
                treeView.Nodes.AddRange(nodes);
            }
        }

        private void ReloadUpDownItems(DomainUpDown domainUpDown, ResourceManager resources)
        {
            if (domainUpDown.Items.Count > 0)
            {
                int selectedIndex = domainUpDown.SelectedIndex;
                this.ReloadItems(domainUpDown.Name, domainUpDown.Items, domainUpDown.Items.Count, resources);
                if (!domainUpDown.Sorted)
                {
                    domainUpDown.SelectedIndex = selectedIndex;
                }
            }
        }

        protected virtual void ScanNonControls(ContainerControl containerControl, ResourceManager resources)
        {
            FieldInfo[] fields = containerControl.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                object obj2 = fields[i].GetValue(containerControl);
                string name = fields[i].Name;
                if (obj2 is MenuItem)
                {
                    MenuItem item = (MenuItem) obj2;
                    item.Enabled = (bool) this.GetSafeValue(resources, name + ".Enabled", item.Enabled);
                    item.Shortcut = (Shortcut) this.GetSafeValue(resources, name + ".Shortcut", item.Shortcut);
                    item.ShowShortcut = (bool) this.GetSafeValue(resources, name + ".ShowShortcut", item.ShowShortcut);
                    item.Text = (string) this.GetSafeValue(resources, name + ".Text", item.Text);
                    item.Visible = (bool) this.GetSafeValue(resources, name + ".Visible", item.Visible);
                }
                if (obj2 is StatusBarPanel)
                {
                    StatusBarPanel panel = (StatusBarPanel) obj2;
                    panel.Alignment = (HorizontalAlignment) this.GetSafeValue(resources, name + ".Alignment", panel.Alignment);
                    panel.Icon = (Icon) this.GetSafeValue(resources, name + ".Icon", panel.Icon);
                    panel.MinWidth = (int) this.GetSafeValue(resources, name + ".MinWidth", panel.MinWidth);
                    panel.Text = (string) this.GetSafeValue(resources, name + ".Text", panel.Text);
                    panel.ToolTipText = (string) this.GetSafeValue(resources, name + ".ToolTipText", panel.ToolTipText);
                    panel.Width = (int) this.GetSafeValue(resources, name + ".Width", panel.Width);
                }
                if (obj2 is ColumnHeader)
                {
                    ColumnHeader header = (ColumnHeader) obj2;
                    header.Text = (string) this.GetSafeValue(resources, name + ".Text", header.Text);
                    header.TextAlign = (HorizontalAlignment) this.GetSafeValue(resources, name + ".TextAlign", header.TextAlign);
                    header.Width = (int) this.GetSafeValue(resources, name + ".Width", header.Width);
                }
                if (obj2 is ToolBarButton)
                {
                    ToolBarButton button = (ToolBarButton) obj2;
                    button.Enabled = (bool) this.GetSafeValue(resources, name + ".Enabled", button.Enabled);
                    button.ImageIndex = (int) this.GetSafeValue(resources, name + ".ImageIndex", button.ImageIndex);
                    button.Text = (string) this.GetSafeValue(resources, name + ".Text", button.Text);
                    button.ToolTipText = (string) this.GetSafeValue(resources, name + ".ToolTipText", button.ToolTipText);
                    button.Visible = (bool) this.GetSafeValue(resources, name + ".Visible", button.Visible);
                }
            }
        }

        private void SetProperty(Control control, string propertyName, ResourceManager resources)
        {
            try
            {
                PropertyInfo property = control.GetType().GetProperty(propertyName);
                if (property != null)
                {
                    string name = control.Name;
                    if (control is Form)
                    {
                        name = "$this";
                    }
                    object obj2 = resources.GetObject(name + "." + propertyName, this.m_cultureInfo);
                    if (obj2 != null)
                    {
                        property.SetValue(control, Convert.ChangeType(obj2, property.PropertyType), null);
                    }
                }
            }
            catch (AmbiguousMatchException exception)
            {
                Trace.WriteLine(exception.ToString());
            }
        }

        public static FormLanguageSwitchSingleton Instance
        {
            get
            {
                return m_instance;
            }
        }
    }
}

